package com.qf.erpb.product.controller;


import com.alibaba.excel.EasyExcel;
import com.qf.erpb.core.common.JsonResult;
import com.qf.erpb.core.vo.PageVO;
import com.qf.erpb.product.entity.XgProduct2;
import com.qf.erpb.product.param.ProductParam;
import com.qf.erpb.product.service.XgProductService;
import com.qf.erpb.product.utils.XgUserReadListener;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/product")
public class XgProductController {
    @Resource
    private XgProductService xgProductService;
    @ApiOperation("单表分页")
    @GetMapping("/page")
    public JsonResult pageProduct(ProductParam param) {
//        param.setPageNum(1);
//        param.setPageSize(5);

        PageVO<XgProduct2> appointmenttypePageVO = xgProductService.pageProduct(param);
        return JsonResult.ok(appointmenttypePageVO);
    }

    @PostMapping("/add")
    public JsonResult addProduct(@RequestBody XgProduct2 xgProduct) {

        xgProductService.save(xgProduct);
        return JsonResult.ok();
    }

    @GetMapping("/delete")
    public JsonResult deleteProduct(Integer id) {

        xgProductService.removeById(id);
        return JsonResult.ok();
    }

    @GetMapping("/getById")
    public JsonResult getById(Integer id) {

        XgProduct2 byId = xgProductService.getById(id);
        return JsonResult.ok(byId);
    }

    @PostMapping("/update")
    public JsonResult updateProduct(@RequestBody XgProduct2 xgProduct) {

        xgProductService.updateById(xgProduct);
        return JsonResult.ok();
    }

    @PostMapping("/deleteBatch")
    public JsonResult deleteRoleBatch(@RequestBody List<Long> ids) {
        xgProductService.removeByIds(ids);
        return JsonResult.ok();
    }

    //=================================
//    @CrossOrigin(origins = "http://localhost:8080")
    @CrossOrigin
    @ApiOperation("excal导入")
    @PostMapping("/import")
    public JsonResult importExcel(MultipartFile file) throws IOException {
        // 别忘了给SysUserReadListener构造方法传入SysUserService对象
        EasyExcel.read(file.getInputStream(),
                XgProduct2.class,
                new XgUserReadListener(xgProductService)).sheet().doRead();
        return JsonResult.ok();
    }
    /**
     * 导出excel
     */
    @ApiOperation("excal导出")
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 调用业务层,查询全部数据
        List<XgProduct2> list = xgProductService.query().list();
        // 下面这个注释是前端使用方式1,即a标签发请求时采用
        // response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("系统用户信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + "info" + ".xlsx");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), XgProduct2.class).autoCloseStream(Boolean.FALSE).sheet("系统用户信息")
                .doWrite(list);
    }


//    /**
//     * 报表展现
//     * @return
//     */
//    @CrossOrigin
//    @GetMapping("/echarts")
//    public JsonResult showEcharts() {
//        // 模拟假数据
//        HashMap<String, Object> map = new HashMap<>( );
//        map.put("sexX", Arrays.asList("男1","女1"));
//        map.put("sexY",Arrays.asList("15","5"));
//        return JsonResult.ok(map);
//    }

    /**
     * 报表展现
     * @return
     */
    @CrossOrigin
    @GetMapping("/echarts")
    public JsonResult showEcharts() {
        List<Map<String,Object>> resultMap = xgProductService.findSex4Charts();
        ArrayList<String> sexList = new ArrayList<>( );
        ArrayList<Object> numList = new ArrayList<>( );
        resultMap.forEach(map -> {
//            String sex = (String)map.get("Stats");
            String sex = String.valueOf(map.get("Stats")); // 使用 String.valueOf() 方法将 Integer 转换为 String

            if (sex.equals("0")){
                sexList.add("展示");
            } else {
                sexList.add("不展示");
            }
            Object num = map.get("num");
            numList.add(num);
        });

        HashMap<String, Object> map = new HashMap<>( );
        map.put("sexX",sexList);
        map.put("sexY",numList);
        /**
         * {
         *     sexX: [展示,不展示],
         *     sexY: [81,29]
         * }
         */
        return JsonResult.ok(map);
    }

//    ==================================================

    /**
     * 报表展现
     * @return
     */
    @CrossOrigin
    @GetMapping("/echarts2")
    public JsonResult showEcharts2() {
        List<Map<String,Object>> resultMap = xgProductService.findSex4Charts2();

        ArrayList<Object> nameList = new ArrayList<>();
        ArrayList<Object> priceList = new ArrayList<>();
        resultMap.forEach(map -> {
            nameList.add(map.get("product_name"));
            priceList.add(map.get("Price"));
        });
// 返回数据格式
        HashMap<String, Object> map = new HashMap<>();
        map.put("sexX", nameList);
        map.put("sexY", priceList);
        return JsonResult.ok(map);
    }

}
